import vue from '@vitejs/plugin-vue'
import {defineConfig} from 'vite'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {NaiveUiResolver} from 'unplugin-vue-components/resolvers'

import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'
import Inspect from 'vite-plugin-inspect'

import path from 'node:path'

const SrcPath = path.resolve(__dirname, 'src')
// https://vitejs.dev/config/
export default defineConfig(({command, mode}) => {
    return {
        // 项目插件
        plugins: [
            vue(),

            AutoImport({
                imports: [
                    'vue',
                    'vue-router',
                    {
                        'naive-ui': [
                            'useDialog',
                            'useMessage',
                            'useNotification',
                            'useLoadingBar',
                            'useThemeVars',
                        ]
                    }
                ],

            }),

            Components({
                resolvers: [
                    // Auto register icon components
                    // Usage <icon-ep-home> / <icon-ep-...>
                    IconsResolver({
                        prefix: 'Icon',
                        enabledCollections: ['ep'],
                        customCollections: ['mm'],
                    }),
                    NaiveUiResolver(),
                ],
            }),

            Icons({
                autoInstall: true,
                customCollections: {
                    // Custom icons
                    // Usage <icon-mm-home> / <icon-mm-...>
                    mm: FileSystemIconLoader(SrcPath + '/assets/icons-svg'),
                },
            }),

            Inspect(),

        ],
        // 基础配置
        base: './',
        publicDir: 'public',
        resolve: {
            alias: {
                '@': SrcPath,
            },
        },
        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        '@border-color-base': '#dce3e8',
                    },
                    javascriptEnabled: true,
                },
            },
        },
        build: {
            outDir: 'dist',
            assetsDir: 'assets',
            assetsInlineLimit: 4096,
            cssCodeSplit: true,
            brotliSize: false,
            sourcemap: false,
            minify: 'terser',
            terserOptions: {
                compress: {
                    // 生产环境去除console及debug
                    drop_console: false,
                    drop_debugger: true,
                },
            },
            rollupOptions: {
                output: {
                    manualChunks: (id) => {
                        if ( id.includes('node_modules/vue') ) {//vue/vue-router
                            return 'vendor';
                        }
                    },
                },
            },

        },
    }
})


